function varargout = rslice_io ( hObject, gdata, command, varargin )
% RSLICE_IO:  various I/O operations
%
% This m-file seemed like a good idea at the time, but has long since
% outlived its usefulness.
%
% Calls:
%     rslice_fix_roms_grid
% CALLED BY:
%     rslice_hslice_redraw, rslice_initialize
%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% $Name: rslice-0_9_2 $
% $Id: rslice_io.m,v 1.13 2005/12/30 18:17:30 jevans Exp $
% AUTHOR:  johnevans@acm.org
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


switch command
case 'file_time'

    %
    % Apparently just retrieve one time step.
    file_time_index = varargin{1};
    ncfile =  gdata.ncfile_list{gdata.ncfile_index};
    if nc_isvar ( ncfile, gdata.record_variable );
        file_time = nc_varget ( ncfile, gdata.record_variable, file_time_index, 1 );
    else
        file_time = file_time_index;
    end

    varargout{1} = file_time;



%
% get the grid information.  
case 'get_grid'

    ncfile = gdata.ncfile_list{1};

    varname = 'theta_s';
    theta_s = nc_varget ( ncfile, varname );
    varname = 'theta_b';
    theta_b = nc_varget ( ncfile, varname );

    varname = 'Tcline';
    Tcline = nc_varget ( ncfile, varname );

    %
    % Try to find either sc_r or s_rho.
    varname = 'sc_r';
    if nc_isvar ( ncfile, 'sc_r' )
        zdim_var = nc_varget ( ncfile, 'sc_r' );
    elseif nc_isvar ( ncfile, 's_rho' )
        zdim_var = nc_varget ( ncfile, 's_rho' );
    else
        fmt = '%s:  nc_varget failed on both sc_r and s_rho, file %s.\n';
        msg = sprintf ( fmt, mfilename, ncfile );
        rslice_error ( msg );
    end
    N = length(zdim_var );

    scoord = [theta_s theta_b Tcline N];

    roms_grid = snc_roms_get_grid ( gdata.ncfile_list{1}, scoord );
    roms_grid = rslice_fix_roms_grid ( roms_grid );
    varargout{1} = roms_grid;


case 'getvarinfo'

    varinfo = [];
    u_varinfo = [];
    v_varinfo = [];
    angle_varinfo = [];


    %
    % retrieve/construct a structure of metadata for the variable in question.
    %
    % If a local file, it is handled with nc_getvarinfo.  For a url, we
    % have to be more careful.
    ncfile =  gdata.ncfile_list{gdata.ncfile_index};
    [varinfo, status] = nc_getvarinfo ( ncfile, gdata.varname );
    if ( status ~= 0 )
        fmt = '%s:  nc_getvarinfo failed on %s, data source %s.\n';
        msg = sprintf ( fmt, mfilename, gdata.varname, ncfile );
        rslice_error ( msg );
    end

    [u_varinfo, status] = nc_getvarinfo ( ncfile, 'u' );
    if ( status ~= 0 )
        fmt = '%s:  nc_getvarinfo failed on u, data source %s.\n';
        msg = sprintf ( fmt, mfilename, ncfile );
        rslice_error ( msg );
    end

    [v_varinfo, status] = nc_getvarinfo ( ncfile, 'v' );
    if ( status ~= 0 )
        fmt = '%s:  nc_getvarinfo failed on v, data source %s.\n';
        msg = sprintf ( fmt, mfilename, ncfile );
        rslice_error ( msg );
    end

    [angle_varinfo, status] = nc_getvarinfo ( ncfile, 'angle' );
    if ( status ~= 0 )
        fmt = '%s:  nc_getvarinfo failed on angle, data source %s.\n';
        msg = sprintf ( fmt, mfilename, ncfile );
        rslice_error ( msg );
    end

    varargout{1} = varinfo;
    varargout{2} = u_varinfo;
    varargout{3} = v_varinfo;
    varargout{4} = angle_varinfo;








case 'title_string'
    title_string = '';
    ncfile =  gdata.ncfile_list{gdata.ncfile_index};
    roms_ncfile_title = nc_attget ( ncfile, nc_global, 'title' );

    title_string = roms_ncfile_title;

    varargout{1} = title_string;











case 'variable_string'
    variable_string = '';
    ncfile =  gdata.ncfile_list{gdata.ncfile_index};

    try
        var_longname = nc_attget ( ncfile, gdata.varname, 'long_name' );
    catch
        var_longname = '';
    end

    try
        var_units = nc_attget ( ncfile, gdata.varname, 'units' );
    catch
        var_units = '';
    end

    variable_string = sprintf ( '%s: (%s)', var_longname, var_units );






    varargout{1} = variable_string;











case 'date_string'

    %
    % Special case
    if strcmp ( gdata.record_variable, 'tide_period' )
        date_string = rslice_tide_period_datestring ( gdata );
        varargout{1} = date_string;
        return
    end

    file_time = varargin{1};
    ncfile =  gdata.ncfile_list{gdata.ncfile_index};
    if nc_isvar ( ncfile, gdata.record_variable )
        base_date = nc_attget ( ncfile, gdata.record_variable, 'units' );
    else
        base_date = '';
    end

    [time_units,r] = strtok ( base_date, ' ' );
    [since,some_date] = strtok ( r, ' ' );
%    [some_date,r] = strtok ( r, ' ' );
    the_date = [];
    if length(some_date) > 0
        [f,date_token_count] = sscanf ( some_date, '%f-%f-%f %f:%f:%f' );
        if ( date_token_count == 6 )
            the_date = datenum ( f(1), f(2), f(3), f(4), f(5), f(6) );
            switch ( time_units )
            case 'days'
                the_date = the_date + file_time;
            case 'hours'
                the_date = the_date + file_time/24;
            case 'minutes'
                the_date = the_date + file_time/1440;
            case 'seconds'
                the_date = the_date + file_time/86400;
            otherwise
                msg = sprintf ( '%s:  unhandled time units %s.\n', mfilename, time_units );
                rslice_warning ( msg );
            end
        else
            msg = sprintf ( '%s:  did not read 6 values from %s.\n', mfilename, some_date );
            rslice_warning ( msg );

        end
    else
        msg = sprintf ( '%s:  could not parse %s.\n', mfilename, base_date );
        rslice_warning ( msg );
    end

    if isempty ( the_date )
        date_string = '';
    else
        date_string = datestr ( the_date,31 );
    end

    varargout{1} = date_string;
        

    










case 'vardata'
    file_time_index = varargin{1};
    handles = guihandles ( hObject );

    depthString = get ( handles.depth_edit, 'String' );

    %
    % Try to parse it.  Is it of the form 's=%d'?  If so, then
    % it is a sigma level.  Otherwise, it must be depth.
    [s,r] = strtok ( depthString, '=' );
    if isempty(r)
        vardepth = str2double ( get ( handles.depth_edit, 'String' ) );
        vardata = hslice_on_depth ( gdata, file_time_index, vardepth );
    else
        [s,r] = strtok ( r, '=' );
        sigma_level = str2double ( s );
        vardata = hslice_on_sigma ( gdata, file_time_index, sigma_level );
    end


    varargout{1} = vardata;










otherwise
    msg = sprintf ( '%s:  unhandled command %s.\n', mfilename, command );
    rslice_error ( msg );
end

return;
